/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import DB_REDIS.Redis_Rd;
import Model.ModelItem;
import Model.Request.ClientRequest;
import Security.Authenticate;
import Security.Scr_Base64;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import libCore.LogUtil;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import libCore.Config;
import share.ShareMacros;

/**
 *
 * @author LinhTA
 */
public class Controller extends HttpServlet {
    
    private static Logger logger_ = Logger.getLogger(Controller.class);

    
     protected void out(String content, HttpServletResponse response) {

        PrintWriter out = null;
        try {
            //response.setContentType("text/html;charset=UTF-8");
            out = response.getWriter();
            out.print(Scr_Base64.Encode(content));
            out.close();
        } catch (Exception ex) {
            //logger_.error("CampainAction.out:" + ex.getMessage(), ex);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
     
     protected void prepareHeader(HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        resp.addHeader("Server", "MAYBAY2");
    }
     
     public void handle(ClientRequest request, HttpServletResponse resp) {}
      
     protected JSONObject defaultResponse_False()
    {
        JSONObject mapjson = new JSONObject();
        mapjson.put(ShareMacros.SUSSCES, "false");
        mapjson.put(ShareMacros.COIN, "-1");
        
        return mapjson;
    }
      protected void outFalse(HttpServletResponse resp)
       {
           JSONObject mapjson = new JSONObject();
            mapjson = defaultResponse_False();
            out(mapjson.toJSONString(), resp);
       }
      
      
    protected boolean pingRedis(HttpServletResponse response)
    {
        String ping = "";
        ping = Redis_Rd.getInstance().ping();
        if(ping != null)
        {
            ping = ping.toLowerCase();
            if(ping.equals("pong"))
                return true;
            else
                return false;
        }
        else
            return false;
        
        
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doProcess(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        try {
			doProcess(req, resp);
		} catch (Exception ex) {
			logger_.error(LogUtil.stackTrace(ex));
		}
    
    }

    protected void doProcess(HttpServletRequest req, HttpServletResponse resp) 
    {
         if(pingRedis(resp))
        {
            
        }
        else
        {
            Test_LogCSV.LogCSV.log("Redis Connect Fail", Config.getParam("redis", "host"));
            JSONObject mapJson = new JSONObject();
             mapJson = defaultResponse_False();
            out(mapJson.toJSONString(), resp);
            
            return;
        }
        
         ClientRequest request = new ClientRequest(req);         
        Authenticate auth = new Authenticate(request._appId,request._sign,request._fbID,request._meID);
      
        if(auth.checkAuth())
        {
           handle(request, resp);
        }
        else
        {
            JSONObject mapJson = new JSONObject();
            mapJson = defaultResponse_False();
            out(mapJson.toJSONString(), resp);
        }
    }
    
}
